package czk.jvm.lecture.gc;

/**
 * 虚拟机
 * -verbose:gc
 * -Xmx200m
 * -Xmn50m
 * -XX:TargetSurvivorRatio=60
 * -XX:+PrintTenuringDistribution 打印堆空间对象的年龄
 * -XX:+PrintGCDateStamps 打印出对来及回收时间戳
 * -XX:+UseConcMarkSweepGC 使用并行标记清理算法
 * -XX:+UseParNewGC 新生代使用的收集器
 * -XX:MaxTenuringThreshold=3 设置来就回收次数晋升为老年代的阈值
 */
public class MyTest4 {
    public static void main(String[] args) throws InterruptedException {
        byte[] bytes=new byte[1024*512];
        byte[] byte1=new byte[1024*512];
        gc();
        Thread.sleep(1000);
        System.out.println(1111);
        gc();
        Thread.sleep(1000);
        System.out.println(222);
        gc();
        Thread.sleep(1000);
        gc();
        System.out.println(333);
        gc();
        System.out.println(444);
        byte[] byte2=new byte[1024*1024];
        byte[] byte3=new byte[1024*1024];
        byte[] byte4=new byte[1024*1024];
        gc();
        Thread.sleep(1000);
        System.out.println(5555);
        gc();
        Thread.sleep(1000);
        System.out.println(6666);
        /**
         * 输出结果
         * 2020-03-02T22:20:10.354+0800: [GC (Allocation Failure)
         * Desired survivor size 3145728 bytes, new threshold 3 (max 3)（超过设置的survivor3m,jvm会计算新的阈值）
         * - age   1:    1691800 bytes,    1691800 total()第一次回收，没被回收的垃圾 年龄加一
         *  40146K->1669K(121856K), 0.0022897 secs] 新生代原来的大小->执行垃圾回收后的大小
         * 1111
         * 2020-03-02T22:20:11.364+0800: [GC (Allocation Failure)
         * Desired survivor size 3145728 bytes, new threshold 3 (max 3)
         * - age   1:     340240 bytes,     340240 total
         * - age   2:    1744512 bytes,    2084752 total
         *  42399K->2299K(121856K), 0.0013374 secs]
         * 222
         * 2020-03-02T22:20:12.380+0800: [GC (Allocation Failure)
         * Desired survivor size 3145728 bytes, new threshold 3 (max 3)
         * - age   2:     339768 bytes,     339768 total
         * - age   3:    1743504 bytes,    2083272 total
         *  42810K->2416K(121856K), 0.0043882 secs]
         * 2020-03-02T22:20:13.397+0800: [GC (Allocation Failure)
         * Desired survivor size 3145728 bytes, new threshold 3 (max 3)
         * - age   3:     339768 bytes,     339768 total
         *  43135K->2573K(121856K), 0.0097971 secs]
         * 333
         * 2020-03-02T22:20:13.414+0800: [GC (Allocation Failure)
         * Desired survivor size 3145728 bytes, new threshold 3 (max 3)
         *  43299K->1991K(121856K), 0.0030719 secs]
         * 444
         * 2020-03-02T22:20:13.422+0800: [GC (Allocation Failure)
         * Desired survivor size 3145728 bytes, new threshold 1 (max 3) （重新计算了阈值，但是不大于3，取得是阈值和年龄较小的那个，所以年龄为1的直接机内老年代）
         * - age   1:    3145776 bytes,    3145776 total
         *  42722K->5063K(121856K), 0.0018281 secs]
         * 5555
         * 2020-03-02T22:20:14.429+0800: [GC (Allocation Failure)
         * Desired survivor size 3145728 bytes, new threshold 3 (max 3)
         *  45797K->5063K(121856K), 0.0034632 secs]
         * 6666
         */
    }
    private static void  gc(){
        for (int i=0;i<40;i++){
            byte[] bytes=new byte[1024*1024];
        }
    }
}
